package com.wk.healthclock;

import java.util.HashMap;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class AlarmDB {

	private SQLiteDatabase alarmWritableDB;

	private SQLiteDatabase alarmReadableDB;

	public AlarmDB() {

	}

	public AlarmDB(Context context) {
		DBHelper dbHelper = new DBHelper(context, "alarm_db", null, 1);
		alarmWritableDB = dbHelper.getWritableDatabase();
		alarmReadableDB = dbHelper.getReadableDatabase();
	}

	public int saveAlarm(AlarmBO alarmBO) {
		ContentValues values = new ContentValues();
		values.put("title", alarmBO.getTitle());
		values.put("week", alarmBO.getWeek());
		values.put("stime", alarmBO.getStime());
		values.put("etime", alarmBO.getEtime());
		values.put("interval", alarmBO.getInterval());
		values.put("frequency", alarmBO.getFrequency());
		values.put("doTimes", alarmBO.getFrequency());
		values.put("undoTimes", alarmBO.getFrequency());
		alarmWritableDB.insert("clock", null, values);
		String selectSql= "select id from clock order by id desc limit 1";
		Cursor cursor = alarmWritableDB.rawQuery(selectSql, null);
		cursor.moveToFirst();
		int primaryId = cursor.getInt(0);
		alarmWritableDB.close();
		return primaryId;
	}
	
	public Map<Integer,String> getTitles(){
		Map<Integer,String> maps = new HashMap<Integer,String>();
		Cursor cursor=alarmReadableDB.query("clock", null, null, null, null, null, null);
		if(cursor.moveToFirst()){
			int id=cursor.getInt(cursor.getColumnIndex("id"));
			String title = cursor.getString(cursor.getColumnIndex("title"));
			maps.put(id, title);
		}
		while(cursor.moveToNext()){
			int id=cursor.getInt(cursor.getColumnIndex("id"));
			String title = cursor.getString(cursor.getColumnIndex("title"));
			maps.put(id, title);
		}
		alarmReadableDB.close();
		return maps;
	}
	
	public AlarmBO getClockById(int id){
		String sql= "select * from clock where id = ?";
		Cursor cursor = alarmReadableDB.rawQuery(sql, new String[]{id+""});
		AlarmBO alarmBO =new AlarmBO();
		cursor.moveToFirst();
		alarmBO.setId(cursor.getInt(cursor.getColumnIndex("id")));
		alarmBO.setWeek(cursor.getString(cursor.getColumnIndex("week")));
		alarmBO.setTitle(cursor.getString(cursor.getColumnIndex("title")));
		alarmBO.setFrequency(cursor.getInt(cursor.getColumnIndex("frequency")));
		alarmBO.setDoTimes(cursor.getInt(cursor.getColumnIndex("doTimes")));
		alarmBO.setUndoTimes(cursor.getInt(cursor.getColumnIndex("undoTimes")));
		alarmReadableDB.close();
		return alarmBO;
	}
	
	public boolean updateByAction(int id,boolean flag){
		String queryTimes = "select doTimes,undoTimes from clock where id = ?";
		Cursor cursor = alarmWritableDB.rawQuery(queryTimes,new String[]{id+""});
		cursor.moveToFirst();
		int doTimes = cursor.getInt(0);
		int undoTimes =cursor.getInt(1);
		if(flag){
			doTimes++;
		}else{
			undoTimes++;
		}
		String updateSql = "update clock set doTimes =?,undoTimes=? where id =?";
		alarmWritableDB.execSQL(updateSql, new Integer[]{doTimes,undoTimes,id});
		alarmWritableDB.close();
		return true;
	}
	
	public boolean deleteById(int id){
		String deleteSql = "delete from clock where id =?";
		alarmWritableDB.execSQL(deleteSql, new Integer[]{id});
		alarmWritableDB.close();
		return true;
	}
	
	
	

}

class DBHelper extends SQLiteOpenHelper {

	public DBHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String createTableSql = "create table clock(" + "id integer primary key autoincrement,"
				+ "title varchar(10)," + "week varchar(20),"
				+ "stime varchar(10)," + "etime varchar(10)," + "interval integer,"
				+ "frequency integer,"
				+ "doTimes integer,"
				+ "undoTimes integer" + ")";
		db.execSQL(createTableSql);

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

}
